﻿using System;
using System.Windows.Forms;

namespace Station.Controls
{
    public partial class WorkOrderCalendar : UserControl
    {
        private Controller _controller;

        #region Properties

        public Controller Controller
        {
            set
            {
                _controller = value;
                _controller.WorkSheetSaved += delegate { SetGridDataSource(); };
                SetGridDataSource();
            }
        }

        #endregion

        public WorkOrderCalendar()
        {
            InitializeComponent();
            schedulerControl.DoubleClick += schedulerControl_DoubleClick;
            Load += WorkOrderCalendar_Load;
            
        }

        void WorkOrderCalendar_Load(object sender, EventArgs e)
        {
            schedulerControl.Start = DateTime.Today;
            schedulerControl.Refresh();
        }


        #region Private Methods

        private void SetGridDataSource()
        {
            folhaObraDataSet.Clear();
            folhaObraDataSet.Merge(_controller.WorkSheets());
        }

        #endregion

        void schedulerControl_DoubleClick(object sender, EventArgs e)
        {
            var action = schedulerControl.SelectedAppointments.Count == 1 ? "Deseja mesmo abrir folha obra?" : "Deseja mesmo efectuar nova folha obra?";
            if (!_controller.ExistsOpenedWorkSheet(this, action))
                return;
            if (schedulerControl.SelectedAppointments.Count == 1)
                _controller.ShowWorkSheet(Convert.ToInt64(schedulerControl.SelectedAppointments[0].ResourceId));
            else
                _controller.NewWorkSheet(schedulerControl.SelectedInterval.Start, schedulerControl.SelectedInterval.End);            
        }

        
    }
}
